loadmod("tablefunc")
local gmt=tablefunc.gmt
module("class",package.seeall)
subfuncsstoreindex="_subfuncs"

classitemset=function(table,key,val)
	if type(val)=="function" or (type(val)=="table" and (gmt(val).__call and gmt(val)[tablefunc.cloneindex])) then
		newsubfunc(table,key,val)
		return
	end
	rawset(table,key,val)
end

newsubfunc=function(table,funcname,func)
	local mt=gmt(table)
	rawset(mt,subfuncsstoreindex,rawget(mt,subfuncsstoreindex) or {})
	rawset(rawget(mt,subfuncsstoreindex),funcname,true)
	rawset(table,funcname,tablefunc.newselfargfunc(table,func))
end

argfunc=function(func)
	local t=getfenv(2)
	return(tablefunc.newselfargfunc(t,func))
end

bindclass=function(table,class)
	if type(table)=="string" then table=_G[table] end
	if type(class)=="string" then table=_G[class] end
	local i,v
	local mt=gmt(table)
	rawset(mt,subfuncsstoreindex,rawget(mt,subfuncsstoreindex)or {})
	local cmt=gmt(class)
	for i,v in pairs(rawget(cmt,subfuncsstoreindex) or {}) do
		newsubfunc(table,i,rawget(class,i))
	end
end

defclassfunc=function()
	if type(newclass)=="string" then newclass=_G[newclass] end
	local t=getfenv(2)
	gmt(t).__newindex=classitemset
end